Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mohammed almakki exercies #88

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

MohamedAlmaki
Copy link

This PR contains the solution of c++ exercise 1 and 2

Copy link

@Harrietbrown Harrietbrown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exercise 1


std::vector<std::string> tokenizeFile(const std::string &file)
{
std::regex re("[-\\s]");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the inent of this that the experssion match a hyphen of a whitespace, but a double backslash exits the special character.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex is used to split the string by two delimiters, the space, and the hyphen. It will match a space or a hyphen. After that, the expression is used in a reverse way, to get all the words that don't match it i.e. the split words.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Hyphen is also a special character and will need to be exited as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will be removed. For example, the string "lines are hard-coded" will be transformed into a vector of ["lines", "are", "hard", "coded"] by this expression.

word.begin(), word.end(), word.begin(),
[](unsigned char c)
{ return std::tolower(c); });
word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task specifies only needing to consider .,?'"!(): you might have considered using regex to strip these away and replacing them with a whitespace at the same time as doing the hyphen.

Copy link
Author

@MohamedAlmaki MohamedAlmaki Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understood, the comparison should be punctation insensitive. So for example, "what's" should be equal to "whats" but replacing it with whitespace will result in the word not being included (not longer than 4). So in this line, I just remove every punctuation mark.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I think you may be right

word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
if (word.length() > 4)
{
wordsCounts[word]++;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should have a check to see if word is currently in the wordCounts map

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, why is it needed?
If there is a get operation, then I need a check, but this is just used as a counter.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimatly it is not required for it to work, but it is better practice to have the check when performing an action on an object in a map where it is not certain that the object will exist.

{
wordsCountsPairs.push_back(pair);
}
std::sort(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good sorting process.

Copy link

@MialLewis MialLewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job - a few comments on the second exercise. Some of the comments apply in more than one place, but I've only pointed out the first occurrence.

exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated Show resolved Hide resolved
Shape(int noOfSides) : noOfSides{noOfSides} {}

private:
int noOfSides;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this exercise, this could be const as there's no requirement to mutate the shapes after construction.

exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated Show resolved Hide resolved
int noOfSides;
};

class Square : public Shape

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have been tempted to have square inherit from rectangle to avoid some code duplication. Override functions where necessary.

exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated Show resolved Hide resolved
exercises-cpp/mohammed_almakki/ex02_oo_basics/src/Shape.h Outdated Show resolved Hide resolved
return "Square";
}

int getSidesCount()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have this defined in the Shape parent class, simply returning its member noOfSides? We then would not have to redefine it in every child.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is really bad. Doesn't know why I did it like this.


void printShapesWithSides(const int &sidesCount)
{
for (auto shape : shapes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely needless complication in this instance, but could be interesting to see if you could reduce code duplication (use of function pointers?)

Copy link
Author

@MohamedAlmaki MohamedAlmaki Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can make a general function that prints shapes that meet a specific condition. The function will accept a lambda filter function as a parameter. The lambda function will accept a parameter of type shape and return a Boolean value. Also, we can get rid of the for loop and use for_each. But I think it is too complex for this small example.

return left->getArea() > right->getArea();
});

for (auto shape : sortedShapes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the above - try to refactor to reduce the code duplication here.

Change types to enum instead of raw strings
Reduce code duplication by abstracting a general printing function
Copy link

@MialLewis MialLewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements - your solution using the lambda filter functions is really neat.

@MohamedAlmaki
Copy link
Author

MohamedAlmaki commented Dec 19, 2022

Thanks for the improvements - your solution using the lambda filter functions is really neat.

Thanks for your great feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants